﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RipPoll.DataAccess;
using RipPoll.Library;

namespace RipPoll.Business
{
    public class PollBO
    {

        public PollPE GetPoll(int pollId)
        {
            RippollEntities re = new RippollEntities();

            PollPE  pe = (from p in re.Polls
                        where p.Id ==  pollId
                        select new PollPE (){ Id = p.Id, ChoiceGroupId = p.ChoiceGroupId, Name = p.Name, Category = new PollCategoryPE{Id = p.CategoryId}, DateAdded = p.DateAdded , DateUpdated = p.DateUpdated, IsApproved = p.IsApproved, ImagePath = p.ImagePath, VideoLink = p.VideoLink}).SingleOrDefault<PollPE>();
            
            if(pe != null)
            {
                pe.Choices = (from c in re.Choices
                              where c.ChoiceGroupId == pe.ChoiceGroupId
                              select new ChoicePE()
                              {
                                   Choice = c.ChoiceName,
                                   ChoiceGroupId = c.ChoiceGroupId,
                                   Id = c.Id,
                                   DateAdded = c.DateAdded,
                                   DateUpdated = c.DateUpdated
                              }).ToList<ChoicePE>();

                PollCategoryPE pcatpe = (from cat in re.PollCategories
                                         where cat.Id == pe.Category.Id
                                         select new PollCategoryPE()
                                         {
                                              Id = cat.Id,
                                              Category = cat.Category,
                                              Description = cat.Description,
                                              DateAdded = cat.DateAdded,
                                              DateUpdated   = cat.DateUpdated
                                         }).SingleOrDefault<PollCategoryPE>();
                if (pcatpe != null)
                    pe.Category = pcatpe;
            }
            return pe;
        }

        public List<PollPE> GetUserPolls(int userId)
        {
            RippollEntities re = new RippollEntities();


            List<PollPE> pList = (from p in re.Polls
                         join up in re.UserPolls on p.Id equals up.PollId
                         where up.UserId == userId
                         select new PollPE() 
                            { 
                                Id = p.Id, 
                                ChoiceGroupId = p.ChoiceGroupId, 
                                Name = p.Name,
                                Category =  new PollCategoryPE(){Id = p.CategoryId},
                                DateAdded = p.DateAdded, 
                                DateUpdated = p.DateUpdated, 
                                IsApproved = p.IsApproved 
                            }).ToList<PollPE>();

            foreach (PollPE p in pList)
            {
                p.Choices = (from c in re.Choices
                           where c.ChoiceGroupId == p.ChoiceGroupId
                           select new ChoicePE()
                              {
                                  Choice = c.ChoiceName,
                                  ChoiceGroupId = c.ChoiceGroupId,
                                  Id = c.Id,
                                  DateAdded = c.DateAdded,
                                  DateUpdated = c.DateUpdated
                              }).ToList<ChoicePE>();
                p.Category = (from pc in re.PollCategories
                              where pc.Id == p.Category.Id
                              select new PollCategoryPE()
                                  {
                                      Category = pc.Category,
                                      Id = pc.Id,
                                      Description = pc.Description,
                                      DateAdded = pc.DateAdded,
                                      DateUpdated = pc.DateUpdated
                                  }
                            ).SingleOrDefault<PollCategoryPE>();
            }
            return pList;
        }


        public string[] GetPollZipCodes(int pollId)
        {
            RippollEntities re = new RippollEntities();

            IQueryable<string> zipcodes = from up in re.UserPolls
                                          join u in re.Users on up.UserId equals u.Id
                                          where up.PollId == pollId
                                          select u.ZipCode;

            return zipcodes.ToArray<string>();
        }

        public void AddPollCategory(PollCategoryPE pc)
        {
            RippollEntities re = new RippollEntities();
            PollCategoryPE pcpe = null;

            IQueryable<PollCategoryPE> v = from pcs in re.PollCategories
                                where pcs.Category.Equals(pc.Category)
                                select new PollCategoryPE() { Id = pcs.Id, Category = pcs.Category, Description = pcs.Description };
            
            pcpe = v.SingleOrDefault<PollCategoryPE>();
            

            if (pcpe == null)
            {
                PollCategory p = new PollCategory();
                p.Description = pc.Description;
                p.Category = pc.Category;                
                re.AddToPollCategories(p);
                re.SaveChanges();
                pc.Id = p.Id;
            }
            else
            {
                pc = pcpe;
            }
        }

        public void AddPoll(PollPE pollPE, UserPE upe)
        {

            RippollEntities re = new RippollEntities();

            if (pollPE.Choices != null && pollPE.Choices.Count > 0 && pollPE.ChoiceGroupId != 1 && pollPE.ChoiceGroupId != 2)
            {
                ChoiceBO cbo = new ChoiceBO();
                cbo.InsertChoices(pollPE.Choices);
                pollPE.ChoiceGroupId = pollPE.Choices[0].ChoiceGroupId;
            }

            Poll p = new Poll() { CategoryId = pollPE.Category.Id, Name = pollPE.Name, ChoiceGroupId = pollPE.ChoiceGroupId, DateAdded = pollPE.DateAdded, VideoLink = pollPE.VideoLink, ImagePath= pollPE.ImagePath };
            re.AddToPolls(p);            
            re.SaveChanges();
            pollPE.Id = p.Id;

            User u = new User(){ Id = upe.Id, City = upe.City, BirthYear = (int)upe.BirthYear, EmailId = upe.Email, Gender = upe.Gender, State = upe.State, ZipCode = upe.ZipCode, FirstName = upe.FirstName, LastName = upe.LastName, DateAdded = upe.DateAdded, DateUpdated = upe.DateAdded , EducationBackgroundId = upe.EducationId, ScreenName = upe.ScreenName };
            UserPoll upoll = new UserPoll() { UserId= u.Id, PollId = p.Id, IsCreated = true, IsTracking=true, SelctedChoiceId=null, DateAdded = DateTime.Now};

            re.UserPolls.AddObject(upoll);            
            re.SaveChanges();

            
        }

        public List<PollCategoryPE> GetPollcategories()
        {
            RippollEntities re = new RippollEntities();
            
            List<PollCategoryPE> pollCats = new List<PollCategoryPE>();

            IQueryable<PollCategoryPE> v = from pcs in re.PollCategories
                                           select new PollCategoryPE() { Category = pcs.Category, Description = pcs.Description, Id = pcs.Id };

            foreach (PollCategoryPE p in v)
                pollCats.Add(p);

            return pollCats;
        }

        public List<PollPE> GetUnApprovedPolls()
        {
            RippollEntities re = new RippollEntities();

            List<PollPE> pollPEs = new List<PollPE>();

            IQueryable<PollPE> v = from p in re.Polls
                                   join c in re.PollCategories on p.CategoryId equals c.Id
                                   where p.IsApproved == false
                                   select new PollPE()
                                   {
                                       Name = p.Name,
                                       Category = new PollCategoryPE(){ Category= c.Category, Description = c.Description, Id = c.Id},
                                       ChoiceGroupId = p.ChoiceGroupId,
                                       Id = p.Id
                                   };

            foreach (PollPE ppe in v)
            {
                ppe.Choices = (from ch in re.Choices
                               where ch.ChoiceGroupId.Equals(ppe.ChoiceGroupId)
                               select new ChoicePE { Choice = ch.ChoiceName, Id = ch.Id, ChoiceGroupId = ch.ChoiceGroupId }).ToList<ChoicePE>();
                pollPEs.Add(ppe);
            }

            return pollPEs;
        }


        public void ApprovePoll(int id)
        {
            RippollEntities re = new RippollEntities();
            re.Polls.Single<Poll>(p => p.Id == id).IsApproved = true;
            re.SaveChanges();

        }
    }
}
